﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using VisionCore;
using PCBase.UIControls;

namespace COER
{
    public partial class FormDieData : Form
    {
        public FormDieData()
        {
            InitializeComponent();

            GetControls(this.Controls);
        }

        private void GetControls(Control.ControlCollection ctrls)
        {
            foreach (Control item in ctrls)
            {
                if (item is Button && item.Tag?.ToString().Split('|')[0] == "btn")
                {
                    ((Button)item).Click += new EventHandler(this.btn_Switch_Click);
                }

                if (item is DLLPC.PCAP.PCButton && item.Tag?.ToString().Split('|')[0] == "btn")
                {
                    ((DLLPC.PCAP.PCButton)item).OP = Kernel.OP;
                }

                if (item.Controls != null)
                {
                    GetControls(item.Controls);
                }
            }
        }

        private int modelNo;
        private DieInfo DieData;
        private string wfID;
        private string laserID;
        public void ShowForm(int idx, DieInfo info)
        {
            DieData = info;
            propertyGrid1.SelectedObject = DieData;
            modelNo = idx;
            switch (modelNo)
            {
                case 1:
                    {
                        this.Text = "载入移载1";
                        wfID = ParameterHelper.LDTR1.Wafer_ID;
                        laserID = ParameterHelper.LDTR1.Laser_ID;
                    }
                    break;
                case 2:
                    {
                        this.Text = "载入移载2";
                        wfID = ParameterHelper.LDTR2.Wafer_ID;
                        laserID = ParameterHelper.LDTR2.Laser_ID;
                    }
                    break;
                case 3:
                    {
                        this.Text = "镭射工位1";
                        wfID = ParameterHelper.LaserST1.Wafer_ID;
                        laserID = ParameterHelper.LaserST1.Laser_ID;
                    }
                    break;
                case 4:
                    {
                        this.Text = "镭射工位2";
                        wfID = ParameterHelper.LaserST2.Wafer_ID;
                        laserID = ParameterHelper.LaserST2.Laser_ID;
                    }
                    break;
                case 5:
                    {
                        this.Text = "镭射工位3";
                        wfID = ParameterHelper.LaserST3.Wafer_ID;
                        laserID = ParameterHelper.LaserST3.Laser_ID;
                    }
                    break;
                case 6:
                    {
                        this.Text = "镭射工位4";
                        wfID = ParameterHelper.LaserST4.Wafer_ID;
                        laserID = ParameterHelper.LaserST4.Laser_ID;
                    }
                    break ; 
                case 7:
                    {
                        this.Text = "镭射工位5";
                        wfID = ParameterHelper.LaserST5.Wafer_ID;
                        laserID = ParameterHelper.LaserST5.Laser_ID;
                    }
                    break;
                case 8:
                    {
                        this.Text = "镭射工位6";
                        wfID = ParameterHelper.LaserST6.Wafer_ID;
                        laserID = ParameterHelper.LaserST6.Laser_ID;
                    }
                    break;
                case 9:
                    {
                        this.Text = "镭射工位7";
                        wfID = ParameterHelper.LaserST7.Wafer_ID;
                        laserID = ParameterHelper.LaserST7.Laser_ID;
                    }
                    break;
                case 10:
                    {
                        this.Text = "镭射工位8";
                        wfID = ParameterHelper.LaserST8.Wafer_ID;
                        laserID = ParameterHelper.LaserST8.Laser_ID;
                    }
                    break;
                case 11:
                    {
                        this.Text = "载出移载1";
                        wfID = ParameterHelper.ULDTR1.Wafer_ID;
                        laserID = ParameterHelper.ULDTR1.Laser_ID;
                    }
                    break;
                case 12:
                    {
                        this.Text = "载出移载2";
                        wfID = ParameterHelper.ULDTR2.Wafer_ID;
                        laserID = ParameterHelper.ULDTR2.Laser_ID;
                    }
                    break;
            }

            this.Show();
            this.timer1.Start();
            this.WindowState = FormWindowState.Normal;
            this.Activate();
        }

        int timecot = 0;
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (this.Visible == false) return;
            btnInspPass.Visible = false;

            btnDel.Monitor(false, ProcedureHelper_EQ.ManualMode);

            btnSave.Visible =(modelNo == 1 || modelNo == 2)&&false;
            tbxWaferID.Visible = (modelNo == 1 || modelNo == 2) && false;
            tbxLaserID.Visible = (modelNo == 1 || modelNo == 2) && false;

            if (!tbxWaferID.Focused)
            {
                tbxWaferID.Text = wfID;
            }
            if (!tbxLaserID.Focused)
            {
                tbxLaserID.Text = laserID;
            }

            if (timecot > 30)
            {
                propertyGrid1.Refresh();
                timecot = 0;
            }
            timecot++;
        }

        private void btn_Switch_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            string[] info = btn.Tag.ToString().Split('|');

            switch (info[1])
            {
                case "Del":
                    {
                        DieData = new DieInfo();
                        switch (modelNo)
                        {
                            case 1:
                                {
                                    ParameterHelper.LDTR1 = DieData;
                                    ProcedureHelper_Exc.SaveLDTR1();
                                }
                                break;
                            case 2:
                                {
                                    ParameterHelper.LDTR2 = DieData;
                                    ProcedureHelper_Exc.SaveLDTR2();
                                }
                                break;
                            case 3:
                                {
                                    ParameterHelper.LaserST1 = DieData;
                                    ProcedureHelper_Exc.SaveLaserST1();
                                }
                                break;
                            case 4:
                                {
                                    ParameterHelper.LaserST2 = DieData;
                                    ProcedureHelper_Exc.SaveLaserST2();
                                }
                                break;
                            case 5:
                                {
                                    ParameterHelper.LaserST3 = DieData;
                                    ProcedureHelper_Exc.SaveLaserST3();
                                }
                                break;
                            case 6:
                                {
                                    ParameterHelper.LaserST4 = DieData;
                                    ProcedureHelper_Exc.SaveLaserST4();
                                }
                                break;
                            case 7:
                                {
                                    ParameterHelper.LaserST5 = DieData;
                                    ProcedureHelper_Exc.SaveLaserST5();
                                }
                                break;
                            case 8:
                                {
                                    ParameterHelper.LaserST6 = DieData;
                                    ProcedureHelper_Exc.SaveLaserST6();
                                }
                                break;
                            case 9:
                                {
                                    ParameterHelper.LaserST7 = DieData;
                                    ProcedureHelper_Exc.SaveLaserST7();
                                }
                                break;
                            case 10:
                                {
                                    ParameterHelper.LaserST8 = DieData;
                                    ProcedureHelper_Exc.SaveLaserST8();
                                }
                                break;
                            case 11:
                                {
                                    ParameterHelper.ULDTR1 = DieData;
                                    ProcedureHelper_Exc.SaveULDTR1();
                                }
                                break;
                            case 12:
                                {
                                    ParameterHelper.ULDTR2 = DieData;
                                    ProcedureHelper_Exc.SaveULDTR2();
                                }
                                break;
                        }
                        this.Visible = false;
                    }
                    break;
                case "Save":
                    {
                        if (modelNo == 1)
                        {
                            //ParameterHelper.LDTR1.Laser_ID = laserID;
                            //ProcedureHelper_Exc.SaveLDTR1();

                        }else if (modelNo == 2)
                        {
                            //ParameterHelper.LDTR2.Laser_ID = laserID;
                            //ProcedureHelper_Exc.SaveLDTR2();
                        }
                    }
                    break;
                case "InspPass":
                    {
                      
                    }
                    break;
            }
        }

        private void FormDieData_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.Visible = false;
            this.timer1.Stop();
            e.Cancel = true;
        }

        private void tbxWaferID_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                wfID = tbxWaferID.Text;
                ActiveControl = null;
            }
        }
        private void tbxLaserID_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                laserID = tbxLaserID.Text;
                ActiveControl = null;
            }
        }
    }
}
